{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 5/5 [00:00<00:00, 25175.89it/s]\n"
     ]
    }
   ],
   "source": [
    "# add path \n",
    "import sys\n",
    "sys.path.append(\"../\")\n",
    "\n",
    "from tqdm import tqdm\n",
    "from exp.utils import *\n",
    "from datasets import load_dataset, Dataset, DatasetDict\n",
    "\n",
    "# Please change this file path\n",
    "all_data = load_dataset_from_file(\"example_instructions_5res_armorm.json\")\n",
    "\n",
    "for data in tqdm(all_data):\n",
    "    rewards_armorm = [item['score'] for item in data['rewards_armorm']]\n",
    "    max_reward_index = rewards_armorm.index(max(rewards_armorm))\n",
    "    min_reward_index = rewards_armorm.index(min(rewards_armorm))\n",
    "    max_reward_response = data['responses'][max_reward_index]\n",
    "    min_reward_response = data['responses'][min_reward_index]\n",
    "\n",
    "    data['chosen'] = [{\"role\": \"user\", \"content\": data['instruction']}, {\"role\": \"assistant\", \"content\": max_reward_response}]\n",
    "    data['rejected'] = [{\"role\": \"user\", \"content\": data['instruction']}, {\"role\": \"assistant\", \"content\": min_reward_response}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'uuid': '8cabba98-e7fa-52f7-a778-ab847f36f123',\n",
       " 'instruction': 'I have code which is sending data to an outgoing web service using POST request. The data is being stored on a RESTful API. However, I want to store this data locally as well, so that even if the web service goes down, I can still use the data locally. This is a real-time application where the data wont be queued and shall be stored directly.',\n",
       " 'gen_input_configs': {'temperature': 1.1, 'top_p': 1.0},\n",
       " 'intent': 'The user wants to store data locally in addition to sending it to a web service, ensuring data availability even if the web service goes down.',\n",
       " 'knowledge': 'To solve this problem, the models need to know about data storage, web services, and real-time data processing.',\n",
       " 'difficulty': 'medium',\n",
       " 'input_quality': 'good',\n",
       " 'quality_explanation': 'The user query is clear and specific about the problem they are trying to solve, which is storing data locally in addition to sending it to a web service. The query also provides some context about the application being real-time and not queuing data. However, the query could be improved by providing more details about the specific issues they are experiencing with their current implementation, such as errors or performance issues. Additionally, the query could benefit from more specific information about the data being sent and the local storage requirements.',\n",
       " 'task_category': 'Coding & Debugging',\n",
       " 'input_length': 344,\n",
       " 'responses': [\"A common pattern in such scenarios is to implement a caching mechanism that stores the data locally, and then periodically synchronizes the local data with the remote web service. This way, you ensure that your application is always accessible even when the remote service is unavailable.\\n\\nHere are some options to consider:\\n\\n1. **Local Database**: Store the data in a local database, such as SQLite or a NoSQL database like SQLite, Realm, or Room. This way, you can easily query and retrieve the data locally.\\n2. **In-memory Data Store**: Use an in-memory data store like Redis or Memcached to store the data. This is useful if you need fast access to the data and can tolerate some data loss in case of a crash.\\n3. **File-based Storage**: Store the data in a file or a set of files on the local file system. This is a simple and lightweight approach, but may not be suitable for large amounts of data.\\n\\nTo implement this, you can follow these steps:\\n\\n1. Send the data to the web service using a POST request.\\n2. Store the data locally in your chosen storage mechanism (database, in-memory data store, or file-based storage).\\n3. Periodically check for new data from the web service and update the local storage.\\n4. When the web service is available, synchronize the local data with the remote data to ensure consistency.\\n\\nHere's some sample Python code using SQLite as an example:\\n```python\\nimport sqlite3\\nimport requests\\n\\n# Connect to the local database\\nconn = sqlite3.connect('data.db')\\ncursor = conn.cursor()\\n\\n# Create table if it doesn't exist\\ncursor.execute('''\\n    CREATE TABLE IF NOT EXISTS data (\\n        id INTEGER PRIMARY KEY,\\n        data TEXT\\n    );\\n''')\\nconn.commit()\\n\\ndef send_data_to_web_service(data):\\n    # Send data to web service using POST request\\n    response = requests.post('https://example.com/api/endpoint', json=data)\\n    if response.status_code == 200:\\n        # Store data locally\\n        cursor.execute('INSERT INTO data (data) VALUES (?)', (data,))\\n        conn.commit()\\n\\ndef sync_data_with_web_service():\\n    # Retrieve data from web service\\n    response = requests.get('https://example.com/api/endpoint')\\n    if response.status_code == 200:\\n        # Update local data\\n        cursor.execute('DELETE FROM data')\\n        for item in response.json():\\n            cursor.execute('INSERT INTO data (data) VALUES (?)', (item,))\\n        conn.commit()\\n\\n# Example usage\\ndata = {'key': 'value'}\\nsend_data_to_web_service(data)\\nsync_data_with_web_service()\\n```\\nThis code sends the data to the web service, stores it locally in a SQLite database, and periodically synchronizes the local data with the remote data. Note that this is a simplified example and you may need to add error handling and other features depending on your specific requirements.\",\n",
       "  \"You can achieve this by storing the data in a local database or a file system, and then sending it to the web service. This way, even if the web service is down, you can still access the data locally. Here's a high-level outline of how you can implement this:\\n\\n1. **Send data to local storage**: Before sending the data to the web service, store it in a local database or file system. You can use a relational database like MySQL or PostgreSQL, or a NoSQL database like MongoDB or Cassandra. Alternatively, you can store the data in a file system using a format like JSON or CSV.\\n2. **Store data in a local database**: You can use a library or framework that provides a simple way to interact with a database. For example, in Python, you can use the `sqlite3` module to store data in a local SQLite database.\\n3. **Store data in a file system**: You can use a library like `json` or `pickle` to store data in a file system. For example, you can store data as a JSON object in a file, or use the `pickle` module to serialize the data to a file.\\n4. **Send data to web service**: After storing the data locally, send it to the web service using a POST request. You can use a library like `requests` or `urllib` to send the request.\\n5. **Handle errors**: If the web service is down, handle the error and store the data locally. You can also retry sending the data to the web service after a certain interval.\\n6. **Sync data**: Periodically sync the local data with the web service. You can do this by sending the local data to the web service, or by retrieving the data from the web service and updating the local copy.\\n\\nHere's some sample Python code that demonstrates how you can store data locally and send it to a web service:\\n```python\\nimport json\\nimport requests\\nimport sqlite3\\n\\n# Connect to the local database\\nconn = sqlite3.connect('data.db')\\nc = conn.cursor()\\n\\n# Create a table to store the data\\nc.execute('CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, data TEXT)')\\n\\n# Store data in the local database\\ndef store_data(data):\\n    c.execute('INSERT INTO data (data) VALUES (?)', (json.dumps(data),))\\n    conn.commit()\\n\\n# Send data to the web service\\ndef send_data_to_web_service(data):\\n    url = 'https://example.com/api/endpoint'\\n    headers = {'Content-Type': 'application/json'}\\n    response = requests.post(url, headers=headers, json=data)\\n\\n    if response.status_code == 200:\\n        print('Data sent successfully')\\n    else:\\n        print('Error sending data:', response.text)\\n\\n# Store and send data\\ndata = {'key': 'value'}\\nstore_data(data)\\nsend_data_to_web_service(data)\\n\\n# Handle errors\\ntry:\\n    send_data_to_web_service(data)\\nexcept requests.exceptions.RequestException as e:\\n    print('Error sending data:', e)\\n\\n# Sync data periodically\\nimport time\\nwhile True:\\n    # Send local data to web service\\n    data = c.execute('SELECT * FROM data').fetchall()\\n    for row in data:\\n        send_data_to_web_service(json.loads(row[1]))\\n    time.sleep(60)  # wait 1 minute before syncing again\\n```\\nNote that this is just a simple example, and you'll need to modify it to fit your specific use case. You may also want to consider using a more robust error handling mechanism, such as retries and exponential backoff, to handle failures when sending data to the web service.\",\n",
       "  \"In that case, you can use a hybrid approach to store the data both on the remote RESTful API and locally. Here are a few strategies you can consider:\\n\\n1. **Store data locally, sync with API**: You can store the data locally in a local database or data structure (e.g., a cache, a database, or even a file) and sync it with the remote API periodically. This way, if the API is unavailable, you can still access the data locally. You can use synchronization mechanisms like ` افت` (async/await) or `setInterval` to periodically check for updates from the API and update the local storage.\\n\\n2. **Store data in a distributed database**: You can use a distributed database that allows you to store data in multiple locations (e.g., a distributed NoSQL database like MongoDB or Cassandra). This way, you can store data both locally and on the remote API, and the data will be synced automatically across all locations.\\n\\n3. **Use a caching layer**: You can use a caching layer like Redis or Memcached to store data temporarily while it's being processed. This way, even if the API is unavailable, the data will be stored locally and can be retrieved later.\\n\\n4. **Store data as a fallback**: You can store a copy of the data locally as a fallback in case the API is unavailable. When the API becomes available again, you can sync the local data with the API.\\n\\nHere's an example of how you could implement the first approach in Node.js:\\n```javascript\\nconst axios = require('axios');\\nconst fs = require('fs');\\n\\n// Set up local storage\\nconst localData = {};\\n\\n// Set up API endpoint\\nconst apiEndpoint = 'https://example.com/api/data';\\n\\n// Set up sync interval\\nconst syncInterval = setInterval(() => {\\n  axios.get(apiEndpoint)\\n    .then(response => {\\n      // Update local data with API data\\n      Object.assign(localData, response.data);\\n      // Save local data to file or database\\n      fs.writeFileSync('localData.json', JSON.stringify(localData));\\n    })\\n    .catch(error => {\\n      console.error('Error syncing data:', error);\\n    });\\n}, 1000 * 60); // Update every 1 minute\\n\\n// Use local data\\nfunction getData(key) {\\n  return localData[key] || null;\\n}\\n```\\nIn this example, the code sets up an interval to sync the local data with the API every minute. If the API is unavailable, the local data will be used instead. When the API becomes available again, the local data will be updated automatically.\\n\\nRemember to adapt this approach to your specific use case and requirements!\",\n",
       "  \"A common use case!\\n\\nTo store data locally and ensure that it's synced with the remote API, you can implement a hybrid approach that combines the following:\\n\\n1. **Cache**: Store the data locally using a caching mechanism, such as a library like Redis or a simple in-memory cache like `HashMap`.\\n2. **Conflict resolution**: Handle conflicts between the local cache and the remote API by implementing a strategy to resolve any inconsistencies.\\n\\nHere's a high-level overview of the approach:\\n\\n**Step 1: Send data to the remote API**\\n\\nYour code sends the data to the remote API using a POST request.\\n\\n**Step 2: Store data locally**\\n\\nImmediately after sending the data to the remote API, store the data locally in your cache (e.g., Redis or HashMap). This ensures that even if the remote API is down, the data is still available locally.\\n\\n**Step 3: Check for conflicts**\\n\\nWhen the remote API responds with an acknowledgement (e.g., 200 OK), update the local cache with the response data. If the response data conflicts with the locally cached data, resolve the conflict using a strategy (e.g., overwrite local data, merge local and remote data, or raise an error).\\n\\n**Step 4: Sync local cache with remote API**\\n\\nSchedule a periodic task to periodically sync the local cache with the remote API. This ensures that any changes made locally or remotely are reflected in both the local cache and the remote API.\\n\\nHere's some sample pseudocode to illustrate this approach:\\n```python\\nimport requests\\nimport redis\\n\\n# Set up Redis cache\\nredis_client = redis.Redis(host='localhost', port=6379, db=0)\\n\\n# Function to send data to remote API and store locally\\ndef send_data(data):\\n    # Send data to remote API\\n    response = requests.post('https://api.example.com/data', json=data)\\n    if response.status_code == 200:\\n        # Update local cache with response data\\n        redis_client.set('data', response.json())\\n    else:\\n        # Handle error\\n        print('Error sending data')\\n\\n# Function to sync local cache with remote API\\ndef sync_data():\\n    # Fetch local cache\\n    local_data = redis_client.get('data')\\n    # Send data to remote API and update local cache with response\\n    response = requests.get('https://api.example.com/data')\\n    if response.status_code == 200:\\n        # Update local cache with response data\\n        redis_client.set('data', response.json())\\n    else:\\n        # Handle error\\n        print('Error syncing data')\\n\\n# Example usage\\ndata_to_send = {'key': 'value'}\\nsend_data(data_to_send)\\n# Periodically sync data\\nsync_data()\\n```\\nThis is a basic example, and you'll need to adapt it to your specific use case. You may also want to consider using a more robust conflict resolution strategy, such as using timestamps or versioning to resolve conflicts.\\n\\nRemember to ensure that your local cache is eventually consistent with the remote API, even if the remote API is down for an extended period. You can achieve this by implementing a retry mechanism or using a more advanced caching strategy.\",\n",
       "  \"To achieve this, you can implement a caching mechanism that stores the sent data locally on your device or server, so that even if the web service is down, you can still access the data locally. Here are a few approaches you can consider:\\n\\n1. **Local Database**: You can store the sent data in a local database, such as SQLite or a NoSQL database like MongoDB. When the web service is available, you can sync the local data with the remote API. When the web service is down, you can still access the data locally.\\n2. **File Storage**: You can store the sent data in a file or a collection of files. This approach is simple, but it may not be suitable for large amounts of data or data that needs to be queried or updated.\\n3. **In-memory Cache**: You can use an in-memory cache like Redis or Memcached to store the sent data. This approach is suitable for small amounts of data and provides fast access, but it may not be suitable for large amounts of data.\\n4. **Local Storage**: You can use local storage mechanisms like HTML5 Storage (localStorage or sessionStorage) to store the sent data. This approach is suitable for web applications and provides a simple way to store small amounts of data.\\n\\nTo implement this, you can modify your code as follows:\\n\\n1. Send the data to the web service using a POST request.\\n2. Store the sent data in your chosen local storage mechanism (database, file, in-memory cache, or local storage).\\n3. When the web service is available, use a separate thread or timer to periodically sync the local data with the remote API. This ensures that the data is updated in both the local storage and the remote API.\\n\\nHere's some sample code in JavaScript to give you an idea of how this could be implemented:\\n```javascript\\n// Send data to the web service\\nfetch('https://example.com/api/endpoint', {\\n  method: 'POST',\\n  headers: {\\n    'Content-Type': 'application/json'\\n  },\\n  body: JSON.stringify(data)\\n});\\n\\n// Store the sent data in local storage\\nlocalStorage.setItem('sentData', JSON.stringify(data));\\n\\n// Set a timer to sync the local data with the remote API periodically\\nsetInterval(() => {\\n  // Fetch the local stored data\\n  const localData = JSON.parse(localStorage.getItem('sentData'));\\n\\n  // Send the data to the web service\\n  fetch('https://example.com/api/endpoint', {\\n    method: 'POST',\\n    headers: {\\n      'Content-Type': 'application/json'\\n    },\\n    body: JSON.stringify(localData)\\n  });\\n}, 60000); // 1 minute interval\\n```\\nNote that this is a basic example, and you should adapt it to your specific requirements and use case. Additionally, you may want to consider implementing error handling and conflict resolution mechanisms to ensure data consistency between the local storage and the remote API.\"],\n",
       " 'gen_response_configs': {'prompt': '<|begin_of_text|><|start_header_id|>user<|end_header_id|>\\n\\nI have code which is sending data to an outgoing web service using POST request. The data is being stored on a RESTful API. However, I want to store this data locally as well, so that even if the web service goes down, I can still use the data locally. This is a real-time application where the data wont be queued and shall be stored directly.<|eot_id|><|start_header_id|>assistant<|end_header_id|>\\n\\n',\n",
       "  'temperature': 0.8,\n",
       "  'top_p': 1.0,\n",
       "  'repetition_penalty': 1.0,\n",
       "  'max_tokens': 4096,\n",
       "  'stop_tokens': ['<|eot_id|>',\n",
       "   '<|end_of_text|>',\n",
       "   '<|start_header_id|>',\n",
       "   '<|end_header_id|>'],\n",
       "  'output_generator': 'meta-llama/Meta-Llama-3-8B-Instruct',\n",
       "  'engine': 'vllm'},\n",
       " 'rewards_armorm': [{'score': 0.11279296875},\n",
       "  {'score': 0.107421875},\n",
       "  {'score': 0.10791015625},\n",
       "  {'score': 0.11669921875},\n",
       "  {'score': 0.11474609375}],\n",
       " 'chosen': [{'role': 'user',\n",
       "   'content': 'I have code which is sending data to an outgoing web service using POST request. The data is being stored on a RESTful API. However, I want to store this data locally as well, so that even if the web service goes down, I can still use the data locally. This is a real-time application where the data wont be queued and shall be stored directly.'},\n",
       "  {'role': 'assistant',\n",
       "   'content': \"A common use case!\\n\\nTo store data locally and ensure that it's synced with the remote API, you can implement a hybrid approach that combines the following:\\n\\n1. **Cache**: Store the data locally using a caching mechanism, such as a library like Redis or a simple in-memory cache like `HashMap`.\\n2. **Conflict resolution**: Handle conflicts between the local cache and the remote API by implementing a strategy to resolve any inconsistencies.\\n\\nHere's a high-level overview of the approach:\\n\\n**Step 1: Send data to the remote API**\\n\\nYour code sends the data to the remote API using a POST request.\\n\\n**Step 2: Store data locally**\\n\\nImmediately after sending the data to the remote API, store the data locally in your cache (e.g., Redis or HashMap). This ensures that even if the remote API is down, the data is still available locally.\\n\\n**Step 3: Check for conflicts**\\n\\nWhen the remote API responds with an acknowledgement (e.g., 200 OK), update the local cache with the response data. If the response data conflicts with the locally cached data, resolve the conflict using a strategy (e.g., overwrite local data, merge local and remote data, or raise an error).\\n\\n**Step 4: Sync local cache with remote API**\\n\\nSchedule a periodic task to periodically sync the local cache with the remote API. This ensures that any changes made locally or remotely are reflected in both the local cache and the remote API.\\n\\nHere's some sample pseudocode to illustrate this approach:\\n```python\\nimport requests\\nimport redis\\n\\n# Set up Redis cache\\nredis_client = redis.Redis(host='localhost', port=6379, db=0)\\n\\n# Function to send data to remote API and store locally\\ndef send_data(data):\\n    # Send data to remote API\\n    response = requests.post('https://api.example.com/data', json=data)\\n    if response.status_code == 200:\\n        # Update local cache with response data\\n        redis_client.set('data', response.json())\\n    else:\\n        # Handle error\\n        print('Error sending data')\\n\\n# Function to sync local cache with remote API\\ndef sync_data():\\n    # Fetch local cache\\n    local_data = redis_client.get('data')\\n    # Send data to remote API and update local cache with response\\n    response = requests.get('https://api.example.com/data')\\n    if response.status_code == 200:\\n        # Update local cache with response data\\n        redis_client.set('data', response.json())\\n    else:\\n        # Handle error\\n        print('Error syncing data')\\n\\n# Example usage\\ndata_to_send = {'key': 'value'}\\nsend_data(data_to_send)\\n# Periodically sync data\\nsync_data()\\n```\\nThis is a basic example, and you'll need to adapt it to your specific use case. You may also want to consider using a more robust conflict resolution strategy, such as using timestamps or versioning to resolve conflicts.\\n\\nRemember to ensure that your local cache is eventually consistent with the remote API, even if the remote API is down for an extended period. You can achieve this by implementing a retry mechanism or using a more advanced caching strategy.\"}],\n",
       " 'rejected': [{'role': 'user',\n",
       "   'content': 'I have code which is sending data to an outgoing web service using POST request. The data is being stored on a RESTful API. However, I want to store this data locally as well, so that even if the web service goes down, I can still use the data locally. This is a real-time application where the data wont be queued and shall be stored directly.'},\n",
       "  {'role': 'assistant',\n",
       "   'content': \"You can achieve this by storing the data in a local database or a file system, and then sending it to the web service. This way, even if the web service is down, you can still access the data locally. Here's a high-level outline of how you can implement this:\\n\\n1. **Send data to local storage**: Before sending the data to the web service, store it in a local database or file system. You can use a relational database like MySQL or PostgreSQL, or a NoSQL database like MongoDB or Cassandra. Alternatively, you can store the data in a file system using a format like JSON or CSV.\\n2. **Store data in a local database**: You can use a library or framework that provides a simple way to interact with a database. For example, in Python, you can use the `sqlite3` module to store data in a local SQLite database.\\n3. **Store data in a file system**: You can use a library like `json` or `pickle` to store data in a file system. For example, you can store data as a JSON object in a file, or use the `pickle` module to serialize the data to a file.\\n4. **Send data to web service**: After storing the data locally, send it to the web service using a POST request. You can use a library like `requests` or `urllib` to send the request.\\n5. **Handle errors**: If the web service is down, handle the error and store the data locally. You can also retry sending the data to the web service after a certain interval.\\n6. **Sync data**: Periodically sync the local data with the web service. You can do this by sending the local data to the web service, or by retrieving the data from the web service and updating the local copy.\\n\\nHere's some sample Python code that demonstrates how you can store data locally and send it to a web service:\\n```python\\nimport json\\nimport requests\\nimport sqlite3\\n\\n# Connect to the local database\\nconn = sqlite3.connect('data.db')\\nc = conn.cursor()\\n\\n# Create a table to store the data\\nc.execute('CREATE TABLE IF NOT EXISTS data (id INTEGER PRIMARY KEY, data TEXT)')\\n\\n# Store data in the local database\\ndef store_data(data):\\n    c.execute('INSERT INTO data (data) VALUES (?)', (json.dumps(data),))\\n    conn.commit()\\n\\n# Send data to the web service\\ndef send_data_to_web_service(data):\\n    url = 'https://example.com/api/endpoint'\\n    headers = {'Content-Type': 'application/json'}\\n    response = requests.post(url, headers=headers, json=data)\\n\\n    if response.status_code == 200:\\n        print('Data sent successfully')\\n    else:\\n        print('Error sending data:', response.text)\\n\\n# Store and send data\\ndata = {'key': 'value'}\\nstore_data(data)\\nsend_data_to_web_service(data)\\n\\n# Handle errors\\ntry:\\n    send_data_to_web_service(data)\\nexcept requests.exceptions.RequestException as e:\\n    print('Error sending data:', e)\\n\\n# Sync data periodically\\nimport time\\nwhile True:\\n    # Send local data to web service\\n    data = c.execute('SELECT * FROM data').fetchall()\\n    for row in data:\\n        send_data_to_web_service(json.loads(row[1]))\\n    time.sleep(60)  # wait 1 minute before syncing again\\n```\\nNote that this is just a simple example, and you'll need to modify it to fit your specific use case. You may also want to consider using a more robust error handling mechanism, such as retries and exponential backoff, to handle failures when sending data to the web service.\"}]}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_data[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "repo_name = \"Magpie-Align/xxxxx\"\n",
    "dataset = Dataset.from_list(all_data)\n",
    "\n",
    "split_dataset = dataset.train_test_split(test_size=0.02)\n",
    "split_dataset.push_to_hub(repo_name)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "magpie",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
